package com.example.demo.exception;

import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author yangzhenyu
 * */
@ToString
public enum ExceptionEnum implements ResultCode {
    INVALID_PARAM(false,"10000","非法参数！"),
    SUCCESS(true,"10002","操作成功！"),
    FAIL(false,"10003","操作失败！"),
    NO_MORE_DATAS(false,"10004","没有数据！"),
    UNAUTHENTICATED(false,"10005","此操作需要登陆系统！"),
    UNAUTHORISE(false,"10006","权限不足，无权操作！"),
    SELECT(false,"10007","查询失败"),
    UPDATE(false,"10008","修改失败"),
    WFUERROR(false,"10009","微服务熔断处理"),
    NULL(false,"10010","参数为空"),
    CONFIG(false,"10011","核心配置错误"),
    UPLOAD(false,"10012","上传失败"),
    DELETE(false,"10013","删除失败"),
    DOWNLOAD(false,"10014","下载失败"),
    SYSCONFIG(false,"10015","系统配置错误"),
    PARAM_ERROR(false,"10016","传参错误"),
    NULL_ERROR(false,"10017","空指针错误"),
    LOCK_ERROR(false,"10018","获取分布式锁失败"),
    SPRING_EVENT_ERROR(false,"10019","Spring 事件监听 失败"),
    DISTRIBUTED_SEESION_ERROR(false,"10020","分布式会话出现错误 失败"),
    INSERT_ERROR(false,"10021","新增失败"),
    BF_ERROR(false,"10022","并发冲突"),
    REPETITION_ERROR(false,"10023","重复提交"),

    ERROR(false,"99999","抱歉，系统繁忙，请稍后重试！");
    /**
     * 操作是否成功
     */
    boolean success;

    /**
     * 枚举编码
     */
    private String code;
    /**
     * 枚举说明
     */
    private String desc;

    ExceptionEnum(boolean success,String code, String desc) {
        this.success = success;
        this.code = code;
        this.desc = desc;
    }

   /**
    * 根据code 查询 desc
    * */
    public static  String getDesc(String code){
        if(StringUtils.isEmpty(code)) {
            return null;
        }
        List<String> list  = new ArrayList<>();
        Arrays.stream(ExceptionEnum.values()).filter(t->t.code.equals(code)).forEach(v->{
            list.add(v.desc);
        });

        return list.size()>0?list.get(0):"";
    }

    @Override
    public boolean success() {
        return success;
    }

    @Override
    public String code() {
        return code;
    }

    @Override
    public String desc() {
        return desc;
    }

    @Override
    public void setDesc(String desc) {
        this.desc = desc;
    }
}
